# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
  def destroy_qype_session
    session[:request_token] = nil
    session[:access_token] = nil
  
    #puts request.env["HTTP_HOST"]
  end

  def create_qype_session
    if session[:request_token].nil?
      consumer = OAuth::Consumer.new(MyQype::API_KEY, MyQype::API_SECRET, { :site => MyQype::API_URL, :authorize_url => MyQype::AUTHORIZE_URL})

      request_token = consumer.get_request_token({:oauth_callback => MyQype::CALLBACK_URL})

      session[:request_token] = request_token

      redirect_to request_token.authorize_url
    else

      if !params[:oauth_verifier].blank? && session[:access_token].nil?
        session[:access_token] = session[:request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
      end
    end

    # http://rdoc.info/rdoc/oauth/oauth-ruby/blob/debec12bfe70072d5708ae8376ed1a313df766d1/OAuth/AccessToken.html
    #request = session[:access_token].request(:get, 'http://api.qype.com/oauth/test_request')
    #place_id = '537582'
    #request = session[:access_token].request(:get, "http://api.qype.com/v1/places/#{place_id}/reviews")
    #puts request.body

    #puts request.env["HTTP_HOST"]
  end

  def login_required
    #puts "DEBUG: login_required"
    puts params.to_json

    if params[:action] = 'new'
    #before the new Review form is shown. We verify that the user is authenticated. In case not he'll be redirectd to qype
      if session[:request_token].nil?
        puts "DEBUG: request_token is nil"
        consumer = OAuth::Consumer.new(MyQype::API_KEY, MyQype::API_SECRET, { :site => MyQype::API_URL, :authorize_url => MyQype::AUTHORIZE_URL})

        case params[:controller]
          when"reviews"
            puts "DEBUG: reviews"
            next_url = MyQype::HOME_URL+"/places/"+params[:id]+"/reviews/new"
          when"places"
            puts "DEBUG: places"
            next_url = MyQype::HOME_URL+"/places/new"
        end

        request_token = consumer.get_request_token({:oauth_callback =>  next_url})
        session[:request_token] = request_token
        redirect_to request_token.authorize_url
      
      else
        puts "DEBUG: session_token is not nil"
        puts params[:oauth_verifier]
        puts session[:access_token]
        if !params[:oauth_verifier].nil? && session[:access_token].nil?
          puts "DEBUG: oauth_verifier not nil and access_token nil"
          session[:access_token] = session[:request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
        elsif
          puts "DEBUG: oaut_verifier nillllllll"
          redirect_to session[:request_token].authorize_url
          flash[:notice] = "Login not successful, please try again."
          session[:request_token] = nil
           #TODO: add some sort of redirect or exit       
        end
      end
    end
  end
  
end

